package swd.basic.system_name_maintain;

import com.pub.database.*;
import java.util.*;

public class Business {
   QueryBean gqbnDAO=null;
   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }
   String selectTableDataToFld(View view) throws Exception {
      String lstrSQL=null;
      Hashtable<String,Object> lhtTableData=null;
      try{
         lstrSQL="select sys_no,sys_name,sys_manager,sys_report_no  from wkf_sys where sys_no='" + view.getsys_no().trim() + "'";
         lhtTableData=gqbnDAO.readSingleRowData(lstrSQL);
         if(lhtTableData==null){
            return "查無資料";
         }
         view.setsys_report_no(String.valueOf((lhtTableData.get("SYS_REPORT_NO").toString())));
         view.setsys_no(String.valueOf((lhtTableData.get("SYS_NO").toString())));
         view.setsys_name(String.valueOf((lhtTableData.get("SYS_NAME").toString())));
      //  view.setsys_manager(String.valueOf((lhtTableData.get("SYS_MANAGER").toString())));
        // view.setsys_manager_name(nameC(lhtTableData.get("SYS_MANAGER").toString().trim()));

       String aa="";
       ArrayList<ArrayList<Object>> rs=gqbnDAO.querySQL("select sys_manager from wkf_sys_owner  where sys_no='"+String.valueOf((lhtTableData.get("SYS_NO").toString()))+"'");
       if (rs!=null){
            for (int i=0;i<rs.size();i++){
               aa+= rs.get(i).toString().replace("[", "").replace("]", "")+",";
            }
        }
         view.setsys_manager(aa);
         return "";
      }
      catch(Exception e){
         throw new Exception(e.toString());
      }
      finally{
         lhtTableData.clear();
         lhtTableData=null;
      }
   }
   String exeUpdSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam=new Vector<Object>();
      try{

         lvtrParam.add(view.getsys_name()==null ? null :String.valueOf(view.getsys_name()));
         //lvtrParam.add(view.getsys_manager()==null ? null :String.valueOf(view.getsys_manager()));
         lvtrParam.add(view.getsys_no()==null ? null :String.valueOf(view.getsys_no()));
         lstrSQL="update wkf_sys set sys_name=?,sys_no=?  where sys_report_no='" + view.getsys_report_no().trim() + "'";
         if(gqbnDAO.executeSQL(lstrSQL,lvtrParam)){
             return "修改成功";
         }else{
             return "修改失敗";
         }

      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e.toString());
      }
      finally{
         lvtrParam.clear();
      }
   }
   String  exeDelSQL(View view){
     String lstrSQL=new String();
     Vector<Object> lvtrParam=new Vector<Object>();
     try{
        lstrSQL="delete wkf_sys " +
                " where sys_no='" + view.getsys_no().trim() + "'";

        if (gqbnDAO.executeSQL(lstrSQL)){
            return "刪除成功";
        }else{
            return "刪除失敗";
        }
     } catch(Exception e){
        e.printStackTrace();
         return "刪除失敗";
     } finally{
         lvtrParam.clear();
     }
  }



   String exeInsSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam=new Vector<Object>();
      try{
         lvtrParam.add((view.getsys_no()==null ? null : view.getsys_no()));
         lvtrParam.add((view.getsys_name()==null ? null : view.getsys_name()));
      //   lvtrParam.add((view.getsys_manager()==null ? null : view.getsys_manager()));
         lvtrParam.add(gqbnDAO.readSingleValueData("select max(sys_report_no)+1 from  wkf_sys"));

         lstrSQL="insert into wkf_sys(sys_no,sys_name,sys_report_no) values (?,?,?)";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){;
            return "新增失敗";
         }
         lvtrParam.clear();

         return "新增成功";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e);
      }
      finally{
         lvtrParam.clear();
      }
   }
   String checkNotNullFld(View view) throws Exception{
       StringBuffer lstbNullFldList=new StringBuffer();
       return (lstbNullFldList.toString().equals("") ? "" : lstbNullFldList.delete(lstbNullFldList.length()-1,lstbNullFldList.length()).toString());
   }
   /*
      nameC
    */
   String nameC(String empno) {
       String sSql =
               "select employee_name_c from pnl_employee where employee_no='" +
               empno + "' and rownum=1";
       String nameC = gqbnDAO.readSingleValueData(sSql);
       return nameC;
   }



}
